package com.autocode.codegenerate.generate.impl;

import com.autocode.codegenerate.database.DbReadTableUtil;
import com.autocode.codegenerate.generate.IGenerate;
import org.apache.commons.lang3.StringUtils;
import com.autocode.codegenerate.generate.impl.ofile.OutPutFile;
import com.autocode.codegenerate.generate.pojo.ColumnVo;
import com.autocode.codegenerate.generate.pojo.onetomany.MainTableVo;
import com.autocode.codegenerate.generate.pojo.onetomany.SubTableVo;
import com.autocode.codegenerate.generate.util.NonceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author qiujunda
 * @date 2019年5月2日
 * @description
 */
public class CodeGenerateOneToMany extends OutPutFile implements IGenerate {
    private static final Logger logger = LoggerFactory.getLogger(CodeGenerateOneToMany.class);
    private static String e;
    public static String a = "A";
    public static String b = "B";
    private MainTableVo f;
    private List<SubTableVo> g;
    private static DbReadTableUtil h = new DbReadTableUtil();

    public CodeGenerateOneToMany(MainTableVo mainTableVo, List<SubTableVo> subTables) {
        this.g = subTables;
        this.f = mainTableVo;
    }

    public Map<String, Object> a() {
        HashMap var1 = new HashMap();
        var1.put("bussiPackage", com.autocode.codegenerate.init.InitConfig.h);
        var1.put("entityPackage", this.f.getEntityPackage());
        var1.put("entityName", this.f.getEntityName());
        var1.put("tableName", this.f.getTableName());
        var1.put("ftl_description", this.f.getFtlDescription());
        var1.put("primaryKeyField", com.autocode.codegenerate.init.InitConfig.m);
        this.f.setFieldRequiredNum(StringUtils.isNotEmpty(com.autocode.codegenerate.init.InitConfig.n)?Integer.parseInt(com.autocode.codegenerate.init.InitConfig.n):-1);
        this.f.setSearchFieldNum(StringUtils.isNotEmpty(com.autocode.codegenerate.init.InitConfig.o)?Integer.parseInt(com.autocode.codegenerate.init.InitConfig.o):-1);
        this.f.setFieldRowNum(Integer.parseInt(com.autocode.codegenerate.init.InitConfig.q));
        var1.put("tableVo", this.f);

        try {
            DbReadTableUtil var10000 = h;
            List var2 = DbReadTableUtil.a(this.f.getTableName());
            var10000 = h;
            List var3 = DbReadTableUtil.b(this.f.getTableName());
            var1.put("columns", var2);
            var1.put("originalColumns", var3);
            Iterator var4 = var3.iterator();

            while(var4.hasNext()) {
                ColumnVo var5 = (ColumnVo)var4.next();
                if(var5.getFieldName().toLowerCase().equals(com.autocode.codegenerate.init.InitConfig.m.toLowerCase())) {
                    var1.put("primaryKeyPolicy", var5.getFieldType());
                }
            }

            var4 = this.g.iterator();

            while(var4.hasNext()) {
                SubTableVo var16 = (SubTableVo)var4.next();
                var10000 = h;
                List var6 = DbReadTableUtil.a(var16.getTableName());
                var10000 = h;
                List var7 = DbReadTableUtil.b(var16.getTableName());
                var16.setColums(var6);
                var16.setOriginalColumns(var7);
                String[] var8 = var16.getForeignKeys();
                ArrayList var9 = new ArrayList();
                String[] var10 = var8;
                int var11 = var8.length;

                for(int var12 = 0; var12 < var11; ++var12) {
                    String var13 = var10[var12];
                    DbReadTableUtil var10001 = h;
                    var9.add(DbReadTableUtil.d(var13));
                }

                var16.setForeignKeys((String[])var9.toArray(new String[0]));
                var16.setOriginalForeignKeys(var8);
            }

            var1.put("subTables", this.g);
        } catch (Exception var14) {
            var14.printStackTrace();
            System.exit(-1);
        }

        long var15 = NonceUtils.c() + NonceUtils.g();
        var1.put("serialVersionUID", String.valueOf(var15));
        logger.info("code template data: " + var1.toString());
        return var1;
    }

    public void generateCodeFile() {
        logger.info("----futurism---Code----Generation----[一对多模型:" + this.f.getTableName() + "]------- 生成中。。。");
        String var1 = com.autocode.codegenerate.init.InitConfig.g;
        Map var2 = this.a();
        String var3 = com.autocode.codegenerate.init.InitConfig.k;
        if(a(var3, "/").equals("code_generate/template")) {
            var3 = "/" + a(var3, "/") + "/onetomany";
        }

        com.autocode.codegenerate.generate.gfile.GenerateFile var4 = new com.autocode.codegenerate.generate.gfile.GenerateFile(var3);

        try {
            this.a(var4, var1, var2);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        logger.info("----futurism----Code----Generation-----[一对多模型：" + this.f.getTableName() + "]------ 生成完成。。。");
    }
}
